import {
  LBSLocation
} from '../../../api/amap'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topNum: 0,
    topNumTwo:0,
    windowHeight: 0,
    status: 0,
    totalHeightOne: 0,
    headerHeight:0,
    indexAddress:'',
    hiddenHeaderType: false,
    searchHeight:0,
    scrollHeight:0
  },
  bindscroll(data){
    var _this = this
    console.log(_this.data.hiddenHeaderType)
    console.log(data)
    if (data.detail.scrollTop >= (_this.data.topNumTwo +30) && _this.data.hiddenHeaderType == false) {
      _this.data.hiddenHeaderType = true
      _this.setData({
        hiddenHeaderType: _this.data.hiddenHeaderType
      })
      console.log('我滚动进来了1')
    } else if (data.detail.scrollTop < (_this.data.topNumTwo-10) && _this.data.hiddenHeaderType == true) {
      _this.data.hiddenHeaderType = false
      _this.setData({
        hiddenHeaderType: _this.data.hiddenHeaderType
      })
      console.log('我滚动进来了2')
    }
  },
  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      status: statusHeight,
      navHeight: navHeight
    })
  },
  contentHeaderClick() {
    var _this = this
    _this.data.scrollHeight = _this.data.topNum
    _this.setData({
      scrollHeight:_this.data.scrollHeight
    })
  },
  addressClick(){
    var _this = this
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      isHighAccuracy:true,
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        LBSLocation({
          lat:res.latitude,
          lng:res.longitude
        }).then(ress=>{
          _this.data.indexAddress = ress.data.address
          wx.setStorageSync('currentAreaId', ress.data.areaId)
          _this.setData({
            indexAddress:_this.data.indexAddress
          }) 
        })
      }
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    _this.setNavSize()
    _this.addressClick()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var _this = this
    this.data.windowHeight = wx.getSystemInfoSync().windowHeight
    this.setData({
      windowHeight:this.data.windowHeight
    })
    var query = wx.createSelectorQuery();
    query.select('.contentHeader').boundingClientRect(function (rect) {
      _this.data.totalHeightOne = _this.data.windowHeight - rect.height
      _this.setData({
        totalHeightOne: _this.data.totalHeightOne,
      })
    }).exec();
    query.select('.searchHeight').boundingClientRect(function (rect) {
      _this.data.searchHeight =  rect.height
      _this.setData({
        searchHeight: _this.data.searchHeight,
      })
    }).exec();
    query.select('.headerBox').boundingClientRect(function (rect) {
      _this.data.topNum =_this.data.totalHeightOne - rect.height+ 36
      _this.data.topNumTwo = _this.data.totalHeightOne - rect.height 
      _this.data.headerHeight = rect.height
      _this.setData({
        topNum: _this.data.topNum,
        topNumTwo:_this.data.topNumTwo,
        headerHeight:_this.data.headerHeight
      })
    }).exec();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this
    _this.setData({
      indexAddress:app.globalData.addresstotalHeightTwo
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


})